/*
 * Copyright © 2019-2020  Whale Cloud, Inc. All Rights Reserved.
 *
 * Notice: Whale Cloud Inc copyrights this specification.
 * No part of this specification may be reproduced in any form or means,
 * without the prior written consent of Whale Cloud Inc.
 *
 */
package com.wteam.modules.system.service.impl;

import com.wteam.modules.system.service.ProvinceService;
import com.wteam.modules.system.domain.Province;
import com.wteam.modules.system.domain.dto.ProvinceDTO;
import com.wteam.modules.system.domain.criteria.ProvinceQueryCriteria;
import com.wteam.modules.system.domain.mapper.ProvinceMapper;
import com.wteam.modules.system.repository.ProvinceRepository;
import com.wteam.exception.BadRequestException;
import com.wteam.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.*;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 省份
 业务实现层.
 *
 * @author herry_wong
 * @since 2020-12-28
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "province")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class ProvinceServiceImpl implements ProvinceService {

    private final ProvinceRepository provinceRepository;

    private final ProvinceMapper provinceMapper;

    private final RedisUtils redisUtils;

    @Override
    public Map<String,Object> queryAll(ProvinceQueryCriteria criteria, Pageable pageable){
        Page<Province> page = provinceRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(provinceMapper::toDto));
    }

    @Override
    public List<ProvinceDTO> queryAll(ProvinceQueryCriteria criteria){
        return provinceMapper.toDto(provinceRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "'id:' + #p0")
    public ProvinceDTO findDTOById(Long id) {
        Province province = provinceRepository.findById(id).orElse(null);
        ValidUtil.notNull(province, Province.ENTITY_NAME, "id", id);
        return provinceMapper.toDto(province);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProvinceDTO create(Province resources) {
        return provinceMapper.toDto(provinceRepository.save(resources));
    }

    @Override
    @CacheEvict(key = "'id:' + #p0.id")
    @Transactional(rollbackFor = Exception.class)
    public void update(Province resources) {
        Province province = provinceRepository.findById(resources.getId()).orElse(null);
        ValidUtil.notNull(province, Province.ENTITY_NAME, "id",resources.getId());

        province.copy(resources);
        provinceRepository.save(province);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(Set<Long> ids) {
        redisUtils.delByKeys("province::id:", ids);
        provinceRepository.logicDeleteInBatchById(ids);
    }

    @Override
    public void download(List<ProvinceDTO> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProvinceDTO province : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("创建时间", province.getCreatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}